import 'package:flutter/material.dart';
import 'package:flutter_eve_plugin/components/ye_xiu/rank/rank_footer.dart';
import 'package:flutter_eve_plugin/components/ye_xiu/rank/rank_header.dart';
import 'package:flutter_eve_plugin/components/ye_xiu/rank/rank_item.dart';
import 'package:flutter_eve_plugin/store/ye_xiu/rank/main.dart';
import 'package:flutter_eve_plugin/utils/screen_util.dart';

class RankItem extends StatefulWidget {
  final BaseRankState rankState;

  RankItem(this.rankState);

  @override
  _RankItemState createState() => _RankItemState();
}

class _RankItemState extends State<RankItem>
    with AutomaticKeepAliveClientMixin {
  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Stack(
      children: <Widget>[
        Padding(
          padding: EdgeInsets.only(bottom: setRatio(60)),
          child: CustomScrollView(
            physics: BouncingScrollPhysics(),
            slivers: <Widget>[
              SliverToBoxAdapter(
                child: RankHeaderWidget(
                  rankUserList: widget.rankState.rankUserList,
                ),
              ),
              SliverFixedExtentList(
                itemExtent: setRatio(60),
                delegate: SliverChildBuilderDelegate((context, index) {
                  return RankItemWidget(
                    index: getRankIndex(index) + 1,
                    rankUser: widget.rankState.rankUserList[getRankIndex(index)],
                  );
                }, childCount: getRankchildCount()),
              ),
            ],
          ),
        ),
        Positioned(
          bottom: 0,
          child: RankFooterWidget(
            selfRank: widget.rankState.selfRank,
          ),
        )
      ],
    );
  }

  int getRankIndex(int index) {
    if (widget.rankState.rankUserList.length > 3) {
      return index + 3;
    }
    return 0;
  }

  ///获取子类数量，自动减去前三名
  int getRankchildCount() {
    if (widget.rankState.rankUserList.isEmpty ||
        widget.rankState.rankUserList.length < 3) {
      return 0;
    } else {
      return widget.rankState.rankUserList.length - 3;
    }
  }

  @override
  bool get wantKeepAlive => true;
}
